package servlet;

import hbase.HbaseLink;
import hbase.HbaseUtil;
import hbase.TheMainTable;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;


@WebServlet("/JspAddServlet")
public class JspAddServlet extends HttpServlet {

    public void init(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TheMainTable studentTable = new TheMainTable(req.getParameter("studentid"), req.getParameter("name"),
                req.getParameter("date"), req.getParameter("phonenumber"),
                req.getParameter("semster"), "A");
        HbaseLink hbaselink = new HbaseLink();


        String id = req.getParameter("studentid");
        String name = req.getParameter("name");
        String phone = req.getParameter("phonenumber");
        String date = req.getParameter("date");
        String ses = req.getParameter("semster");
        String status = req.getParameter("");

//        List<String> obj_value = (new UserLogFromHBase()).getUserLogList("Student", new String[]{"StuInfo","Grades",});

        ArrayList<TheMainTable> studentArrayList = new ArrayList<>();
        TheMainTable student = new TheMainTable(id, name, date, phone, ses, status);

        studentArrayList.add(student);


        HbaseUtil hbaseAddUtil = new HbaseUtil();
        String obj_value = "";
        try {

            obj_value = hbaseAddUtil.insertinfo("niit01", "2181", "student", studentArrayList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw =  resp.getWriter();
        pw.write("<h1>"+  obj_value);




//        int judge = AddStudent.add(studentTable);
//        init(req,resp);
//        if (judge==0){
//            req.getRequestDispatcher("AddSuccess.jsp").forward(req,resp);
//        }
//        else{
//            req.getRequestDispatcher("AddFail.jsp").forward(req,resp);
//        }
    }

        @Override
        protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }


}
